package com.medical.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.mapper.DeptMapper;
import com.medical.mapper.EmployeeMapper;
import com.medical.model.entity.Dept;
import com.medical.model.entity.Employee;
import com.medical.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    private final DeptMapper deptMapper;

    public EmployeeServiceImpl(DeptMapper deptMapper) {
        this.deptMapper = deptMapper;
    }

    @Override
    public List<Employee> listEmployeesWithDept() {
        return list().stream().map(employee -> {
            Employee ro = new Employee();
            org.springframework.beans.BeanUtils.copyProperties(employee, ro);

            Dept dept = deptMapper.selectById(employee.getDeptId());
            ro.setDeptName(dept != null ? dept.getName() : "未分配");

            return ro;
        }).collect(Collectors.toList());
    }

    @Override
    public Employee getEmployeeWithDeptById(Long id) {
        Employee employee = getById(id);
        if (employee == null) return null;

        Employee ro = new Employee();
        BeanUtils.copyProperties(ro, employee);

        Dept dept = deptMapper.selectById(employee.getDeptId());
        ro.setDeptName(dept != null ? dept.getName() : "未分配");

        return ro;
    }
}